package com.iwuke.dance.wallet;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.iwuke.dance.R;
import com.iwuke.dance.activity.BaseActivity;
import com.iwuke.dance.gadget_view.TitleBarView;
import com.iwuke.dance.http_request.Bean;
import com.iwuke.dance.http_request.PathUrl;
import com.iwuke.dance.util.PhoneMaskUtil;
import com.iwuke.dance.util.ToastUtil;
import com.iwuke.dance.util.Util;
import com.iwuke.dance.util.independent_edit_text.IndependentEditTextUtil;
import com.iwuke.dance.util.merchants_connect.NetworkEvent;

import java.util.HashMap;

import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * @author: 魏从响
 * @date: 2025/9/16
 * 设置支付密码
 */
public class SetPaymentPasswordActivity extends BaseActivity implements View.OnClickListener {

    private TitleBarView title_bar;
    private String password;
    private TextView tv_desc;
    private IndependentEditTextUtil independentEditTextUtil;


    /**
     * @description 网络实时监听
     */
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onEvent(NetworkEvent networkEvent){
        BaseActivity.setConnect(SetPaymentPasswordActivity.this,networkEvent,title_bar);

    }

    @Override
    protected int getView() {
        return R.layout.activity_set_payment_password;
    }

    @Override
    protected void init(View mView) {

        title_bar = findViewById(R.id.title_bar);
        independentEditTextUtil = new IndependentEditTextUtil(this, mView);

        //描述
        tv_desc = findViewById(R.id.tv_desc);

        title_bar.setTitleText("设置支付密码");
        title_bar.setLeftOnClick(this);

    }

    @Override
    protected void initData() {

//        Bean data = (Bean) getIntent().getSerializableExtra("data");
        Bean data = Util.getSerializableSafe(getIntent(), "data", Bean.class);
//        LogUtil.logD("身份id",data.id+"");

        independentEditTextUtil.setOnEditTextClick(new IndependentEditTextUtil.OnEditTextClick() {
            @Override
            public void onEditTextClick(String value) {
                if (TextUtils.isEmpty(password)){
                    if (!PhoneMaskUtil.hasThreeSameDigits(value)){
                        //密码成功
                        tv_desc.setText("确认支付密码");
                        password = value;
                        independentEditTextUtil.deleteData();//删除数据
                    }else {
                        ToastUtil.setToast(SetPaymentPasswordActivity.this,"密码不能重复，连续的数字！");
                    }
                }else {
                    if (password.equals(value)){

                        HashMap<String,Object> map = new HashMap<>();
                        map.put("walletId",data.id);
                        map.put("password",value);

                        PathUrl.createByPassword(map, new PathUrl.DataCallBack<Bean>() {
                            @Override
                            public void onSucess(Bean bean) {
                                //绑定本人身份信息
                                Util.setIntent(SetPaymentPasswordActivity.this, PasswordSuccessfulActivity.class);
                                finish();
                            }

                            @Override
                            public void onFail(String errorCode) {
                                ToastUtil.setToast(SetPaymentPasswordActivity.this,errorCode);
                            }
                        });


                    }else {
                        //密码错误
                        tv_desc.setText("设置6位支付密码");
                        password = "";
                        independentEditTextUtil.deleteData();//删除数据
                        ToastUtil.setToast(SetPaymentPasswordActivity.this,"密码不一致，请重新输入！");
                    }
                }

            }

            @Override
            public void onResendTimeClick() {

            }
        });

    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.img_left){
            finish();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (independentEditTextUtil!=null){
            independentEditTextUtil.onDestroy();
        }
        if (independentEditTextUtil!=null){
            independentEditTextUtil=null;
        }
    }
}
